from PySide.QtCore import Qt
from PySide.QtGui import QApplication, QWidget, QListView
from PySide.QtGui import QVBoxLayout, QHBoxLayout, QSizePolicy

import model
import contents
from videodialog import KhanTube

class KhanTubeBrowser(QWidget):
    def __init__(self, playlist_model=None):
        QWidget.__init__(self)
        self.setWindowTitle('KhanTubeBrowser')
        self.resize(1000, 600)

        verticalLayout = QVBoxLayout(self)
        horizontalLayout = QHBoxLayout()

        self.playlistView = QListView(self)
        self.playlistView.setWordWrap(True)
        self.playlistView.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
        self.playlistView.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

        self.playlistView.setModel(playlist_model)

        horizontalLayout.addWidget(self.playlistView)

        self.videolistView = QListView(self)
        self.videolistView.setWordWrap(True)
        self.videolistView.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.videolistView.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

        horizontalLayout.addWidget(self.videolistView)

        verticalLayout.addLayout(horizontalLayout)

        self.playlistView.clicked.connect(self.playlist_row_clicked)
        self.videolistView.clicked.connect(self.videolist_row_clicked)

    def playlist_row_clicked(self, index):
        self.videolistView.setModel(self.playlistView.model().getVideoListModel(index))

    def videolist_row_clicked(self, index):
        tube = KhanTube()
        tube.exec_()
        print 'KhanTubeBrowser.videolist_row_clicked'
        print index
        print


def main():
    app = QApplication([])
    app.setApplicationName('KhanTube')
    playlist_model = model.PlaylistModel(contents.loadPlaylists())
    ui = KhanTubeBrowser(playlist_model)
    ui.show()
    app.exec_()


if __name__ == '__main__':
    main()

